# auther：wanghao
import time

from case.base.basepage import BasePage
from case.base import route
from case.pages.storepage import StorePage

"""
首页我的页面
"""
class MinePage(BasePage):
    """
    首页直播tab公共方法
    """
    locators = {
        "BASE_LIVING_IMAGE": "image.avatar-index--image",
        "BASE_MINE_NAME": "view.nickname",
        "BASE_MINE_MESSAGE": "view.component-index--left-icon.component-index--sprite-0"
    }

    """
    首页我的页面
    """

    def to_mine_page(self):
        self.mini.page.get_element("view.index--tab-bar-item.index--index-3").click()

    """
    判断从首页点击我的模块，判断跳转路径是否正确
    """

    def check_mine_page(self):
        self.mini.assertEqual(self.current_path(), route.mine_route)

    """
    我的模块元素check
    """

    def check_mine_element(self):
        # 判断页面是否有小店名称模块
        self.mini.assertTrue(self.mini.page.element_is_exists("view.component-index--nickname"))
        # 判断页面是否有消息模块
        self.mini.assertTrue(self.mini.page.element_is_exists("view.component-index--left-icon.component-index--sprite-0"))

    """
    进入全部订单check订单是否存在
    """

    def check_all_tabs_order(self):
        # 生成一个爱逛订单

        # 点击【全部订单】进入订单页面
        all_order_element = self.mini.page.get_element("view.component-index--all-order__desc")
        all_order_element.click()
        time.sleep(3)
        # 判断全部Tab有订单存在
        self.mini.assertTrue(self.mini.page.element_is_exists(
            "view.title-index--goods-card-title.van-multi-ellipsis--l2.item-index--gb-order-item__card-title", 5))
        self.check_order_exists("ALL")
        # 获取到所有订单tab
        order_tabs = self.mini.page.get_elements("view.van-ellipsis.tabs-index--van-ellipsis")
        # 进入待付款Tab是否有订单存在
        order_tabs[2].click()
        self.check_order_exists("TOPAY")
        # 进入待发货Tab是否有订单存在
        order_tabs[4].click()
        self.check_order_exists("PAID")
        # 进入待收货Tab是否有订单存在
        order_tabs[6].click()
        self.check_order_exists("SENT")

    """
    判断该Tab下是否有订单存在
    """

    def check_order_exists(self, tab):
        # 判断是否有订单
        order_status = self.mini.page.wait_for(
            "order-list[is='packages/order/my-order/components/list/index'][id='%s'] >>> gb-order-item[is='packages/order/components/order/gb-order-item/index'] >>> goods-title[is='components/e-core/goods-card/title/index']" % tab)
        if order_status:
            self.into_order_detail_page(tab)
            self.mini.app.navigate_back()
            time.sleep(1)
        else:
            self.mini.assertTrue(self.mini.page.element_is_exists(
                "view.list-index--order-list__load-no-next-text"))

    """
    进入订单详情页面
    """

    def into_order_detail_page(self, tab):
        # 获取订单商品名称
        time.sleep(1)
        good_name_element = self.mini.page.get_element(
            "order-list[is='packages/order/my-order/components/list/index'][id='%s']" % tab) \
            .get_element("gb-order-item[is='packages/order/components/order/gb-order-item/index']") \
            .get_element("goods-title[is='components/e-core/goods-card/title/index']")
        good_name = good_name_element.inner_text
        # 进入订单详情
        good_name_element.click()
        if tab == "ALL" or tab == "TOPAY":
            # 订单号是否存在
            self.mini.assertTrue(self.mini.page.element_is_exists(
            "view.info-index--order-info__item__value", 3))
            # 对比订单详情页商品名称
            good_name_detail_element = self.mini.page.get_element(
                "view.title-index--goods-card-title.van-multi-ellipsis--l2.item-index--gb-order-item__card-title")
            good_name_detail = good_name_detail_element.inner_text
            self.mini.assertEqual(good_name, good_name_detail, "订单详情页对比商品名称成功")

    """
    获取第N个订单的订单号和订单商品名称
    """

    def get_order_no_good_title(self, tab, index):
        # 获取订单商品名称
        time.sleep(1)
        good_name_element = \
        self.mini.page.get_element("order-list[is='packages/order/my-order/components/list/index'][id='%s']" % tab) \
            .get_elements("gb-order-item[is='packages/order/components/order/gb-order-item/index']")[index] \
            .get_element("goods-title[is='components/e-core/goods-card/title/index']")
        good_name = good_name_element.inner_text
        # 进入订单详情
        good_name_element.click()
        # 订单号是否存在
        order_no = self.mini.page.get_element(
            "view.info-index--order-info__item__value").inner_text
        return order_no, good_name

    """
    通过订单进入小店页面
    """

    def into_store_from_order(self):
        # 通过订单进入小店页面
        store_name_element = self.mini.page.get_element(
            "view.item-index--header-title.item-index--header-title-with-arrow.van-ellipsis")
        store_name_element.click()
        time.sleep(1)
        # 校验小店名称
        self.mini.assertTrue(self.mini.page.element_is_exists("view.shop-name.certification"))
        goods_list = self.mini.page.get_elements("view.goods-item-index--goods-name")
        assert len(goods_list) > 0
        return goods_list

    """
    通过订单->小店->商详页面
    """

    def into_good_detail_from_store(self, goods_list):
        # 通过订单进入小店页面
        # 获取第一个商品的名称
        good_title = goods_list[0].inner_text
        # 进入商详页
        goods_list[0].click()
        time.sleep(1)
        # 获取商详页商品的名称
        good_title_detail = self.mini.page.get_element("view.title-index--title.van-multi-ellipsis--l2").inner_text
        self.mini.assertIn(good_title, good_title_detail, "对比商品名称失败")

    """
    进入商详通过：订单->小店->商详页面
    """

    def check_good_detail(self):
        # 点击【全部订单】进入订单页面
        all_order_element = self.mini.page.get_element("view.component-index--all-order__desc")
        all_order_element.click()
        time.sleep(1.5)
        # 判断全部Tab有订单存在
        self.mini.assertTrue(self.mini.page.element_is_exists(
            "view.title-index--goods-card-title.van-multi-ellipsis--l2.item-index--gb-order-item__card-title"))
        self.check_order_exists("ALL")
        # 进入小店
        goods_lists = self.into_store_from_order()
        # 通过订单->小店->商详页面
        self.into_good_detail_from_store(goods_lists)

    """
    订单列表搜索功能check
    """

    def check_search_order(self, tab, search_type):
        # 点击【全部订单】进入订单页面
        all_order_element = self.mini.page.get_element("view.component-index--all-order__desc")
        all_order_element.click()
        time.sleep(2)
        # 判断全部Tab有订单存在
        self.mini.assertTrue(self.mini.page.element_is_exists(
            "view.title-index--goods-card-title.van-multi-ellipsis--l2.item-index--gb-order-item__card-title"))
        # 判断是否有订单
        order_status = self.mini.page.wait_for(
            "order-list[is='packages/order/my-order/components/list/index'][id='%s'] >>> gb-order-item[is='packages/order/components/order/gb-order-item/index'] >>> goods-title[is='components/e-core/goods-card/title/index']" % tab)
        if order_status:
            order_no, good_name = self.get_order_no_good_title("ALL", 1)
            self.mini.app.navigate_back()
            time.sleep(1)
            if search_type == "order_no":
                self.search_order_no(order_no)
                self.check_search_order_no_result(tab, order_no, good_name)
            else:
                self.search_order_no(good_name)
                self.check_search_good_name_result(tab, good_name)
        else:
            self.mini.assertTrue(self.mini.page.element_is_exists(
                "view.list-index--order-list__load-no-next-text"))

    """
    搜索订单号
    """

    def search_order_no(self, search_value):
        # 输入搜索关键字
        search_input_element = self.mini.page.get_element("input.search-bar-index--input")
        search_input_element.input(search_value)
        search_input_element.trigger("confirm", {"value": search_value})
        time.sleep(1)
        self.mini.page.wait_for("order-list[is='packages/order/my-order/components/list/index'][id='ALL']", 10)


    """
    进入第一个商品商详页对比搜索订单号的结果
    """

    def check_search_order_no_result(self, tab, order_no_expected, good_name_expected):
        # 获取订单商品名称
        time.sleep(1)
        good_name_element = \
            self.mini.page.get_element("order-list[is='packages/order/my-order/components/list/index'][id='%s']" % tab) \
                .get_elements("gb-order-item[is='packages/order/components/order/gb-order-item/index']")[0] \
                .get_element("goods-title[is='components/e-core/goods-card/title/index']")
        # 进入订单详情
        good_name_element.click()
        # 对比订单号
        order_no = self.mini.page.get_element(
            "view.info-index--order-info__item__value").inner_text
        self.mini.assertEqual(order_no_expected, order_no, "对比订单号失败")
        # 对比订单详情页商品名称
        good_name_detail_element = self.mini.page.get_element(
            "view.title-index--goods-card-title.van-multi-ellipsis--l2.item-index--gb-order-item__card-title")
        good_name_detail = good_name_detail_element.inner_text
        self.mini.assertEqual(good_name_expected, good_name_detail, "对比订单商品名称失败")
        return self

    """
    进入前4个商品商详页对比搜索商品名称的结果
    """

    def check_search_good_name_result(self, tab, good_name_expected):
        # 获取订单商品名称
        time.sleep(2)
        good_name_elements = \
            self.mini.page.get_element("order-list[is='packages/order/my-order/components/list/index'][id='%s']" % tab) \
                .get_elements("gb-order-item[is='packages/order/components/order/gb-order-item/index']")
        for i in range(0, len(good_name_elements), 1):
            if i < 4:
                good_name = good_name_elements[i].get_element(
                    "goods-title[is='components/e-core/goods-card/title/index']").inner_text
                self.mini.assertEqual(good_name_expected, good_name, "对比订单商品名称失败")
            else:
                break
        return self
